﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Castle.Core.Logging;

namespace TVN.ShopMan.ApplicationInterface.AppForms
{
    using TVN.Common.Interfaces;
    using TVN.ShopMan.ActiveModels;
    using TVN.ShopMan.ApplicationInterface.Helpers;

    public partial class RepositoryFormEdit : Form
    {
        /// <summary>
        /// All the modes of editing an account.
        /// </summary>
        public enum EditModes
        {
            Default,
            OwnerFocus
        }

        private static readonly ILogger _logger = Program.Logger.CreateChildLogger("RepositoryFormEdit");
        private IRepository<Repository> repositoryRepository = null;
        private IRepository<Account> accountRepository = null;
        private Repository selectedRepository = null;
        private EditModes editMode = EditModes.Default;

        #region Properties

        public Repository SelectedRepository
        {
            get { return this.selectedRepository; }
            set { this.selectedRepository = value; }
        }

        #endregion

        public RepositoryFormEdit()
        {
            InitializeComponent();

            this.SetUp();
            this.LoadData();
        }

        public RepositoryFormEdit(Repository repositoryItem, EditModes mode)
        {
            InitializeComponent();

            this.SetUp();

            this.editMode = mode;
            this.selectedRepository = repositoryItem;

            this.LoadData();
        }

        #region Private methods

        private void SetUp()
        {
            this.repositoryRepository = Program.Container.Resolve<IRepository<Repository>>();
            this.accountRepository = Program.Container.Resolve<IRepository<Account>>();
        }

        private void LoadData()
        {
            this.cboOwner.DataSource = this.accountRepository.FindAll();

            if (this.selectedRepository != null)
            {
                this.txtAddress.Text = this.selectedRepository.Address;
                this.txtEmail.Text = this.selectedRepository.Email;
                this.txtName.Text = this.selectedRepository.Name;
                this.dtpStartDate.Value = this.selectedRepository.StartDate;
                this.ckbActive.Checked = this.selectedRepository.IsActive;
                this.cboOwner.SelectedItem = this.selectedRepository.Owner;
            }
        }

        #endregion

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool isCreating = ((this.selectedRepository == null) || (this.selectedRepository.Id <= 0));
            if (AppHelper<Repository>.IsExists(Repository.Properties.Name, this.txtName.Name, isCreating))
            {
                this.txtName.Focus();
            }
            else
            {
                this.AssignRepositoryData();
                this.DialogResult = DialogResult.OK;
            }
        }

        private void AssignRepositoryData()
        {
            try
            {
                if (this.selectedRepository == null)
                {
                    this.selectedRepository = new Repository
                    {
                        CreatedDate = DateTime.Now,
                        Creator = Program.SessionData.ActiveAccount,
                    };
                }
                else
                {
                    this.selectedRepository.LastModifiedDate = DateTime.Now;
                    this.selectedRepository.LastModifier = Program.SessionData.ActiveAccount;
                }

                this.selectedRepository.Address = this.txtAddress.Text;
                this.selectedRepository.Email = this.txtEmail.Text;
                this.selectedRepository.IsActive = this.ckbActive.Checked;
                this.selectedRepository.Name = this.txtName.Text;
                this.selectedRepository.Owner = (Account)this.cboOwner.SelectedItem;
                this.selectedRepository.StartDate = this.dtpStartDate.Value;
            }
            catch (Exception ex)
            {
                _logger.Error("Could not save unit", ex);
            }
        }
    }
}
